﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MpSoft.Library.BUSS.Customers;
using MpSoft.Library.DTO;
using MpSoft.Basic.Utils;

namespace MpSoft.Web.Controls.Customer
{
    public partial class PollControl : System.Web.UI.UserControl
    {
        #region Var

        #endregion
        
        #region Method

        private void BindControl()
        {
            var listPoll = PollBUSS.Poll_List(-1, 1);
            if (listPoll != null)
            {
                var poll = listPoll.FirstOrDefault();
                pollName.InnerText = poll.Name;

                var pollAnswer = PollAnswerBUSS.PollAnswer_ByPollID(poll.PollID);
                rblPollAnswers.DataSource = pollAnswer;
                rblPollAnswers.DataBind();
            }
        }

        #endregion
        
        #region Event
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindControl();
        }

        #endregion

        protected void cmdPoll_Click(object sender, EventArgs e)
        {
            int value;
            int.TryParse(rblPollAnswers.SelectedItem.Value, out value);
            var pollanswer = PollAnswerBUSS.PollAnswer_ByID(value);
            if (pollanswer != null)
            {
                pollanswer.Count = pollanswer.Count + 1;
                if (PollAnswerBUSS.PollAnswer_Update(pollanswer) > 0)
                    Response.Redirect(SEOHelper.GetCategoryUrl("tham-do-y-kien"));
            }
        }

        #region Properties

        #endregion
    }
}